package com.common.supply.controller;

import com.common.supply.config.dynamicdb.DynamicDbConstant;
import com.common.supply.config.dynamicdb.DynamicMultipleDataSource;
import com.common.supply.domain.Order;
import com.common.supply.component.rocketmq.producer.*;
import com.common.supply.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * swagger app前端接口
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/app")
public class TestController {

    @Autowired
    private SpringProducer producer;

    @Autowired
    private OrderService orderService;

    @Autowired
    private DynamicMultipleDataSource dataSource;


    @GetMapping("/send")
    public void getDefaultProductList(String testTopic, String message) {
        producer.sendMessage(testTopic, message);
    }

    /**
     * 顺序消息
     */
    @GetMapping("/sendOrder")
    public void sendOrder() {
        producer.sendQueueMessage();
    }

    /**
     * 顺序消息
     */
//    @PostConstruct
    @GetMapping("/orders")
    public void orders(int type) {
        databaseSwitch(type);
        List<Order> list = orderService.list();
        System.err.println(list.toString());
    }

    private void databaseSwitch(int type) {
        if (type == 1) {
            //ExtSource
            dataSource.setDataSourceKey(DynamicDbConstant.EXT_DATA_SOURCE);
        }
        if (type == 2) {
            //Dev
            dataSource.setDataSourceKey(DynamicDbConstant.LINK_CREDIT_SOURCE);
        }
        if (type == 3) {
            //Sit
            dataSource.setDataSourceKey(DynamicDbConstant.POWER_KASH_SOURCE);
        }
    }
}
